package com.edu.edu.day9_19_week7_homework.thread;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Administrator on 2016/9/2.
 */
public class HttpUtil {

    public static final  byte[] loadData(String params){
        try {
            URL url = new URL(params);
           HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if(conn.getResponseCode()==200){
                InputStream is = conn.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int len = 0;
                byte[] buffer  = new byte[1024];
                while((len = is.read(buffer)) != -1){
                    baos.write(buffer,0,len);
                    baos.flush();
                }
                byte[] data = baos.toByteArray();
                return data;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
